﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/11 21:38:52
 * ************************************************************/
namespace Mozlite.FileStorages
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 用户文件夹。
    /// </summary>
    public class UserFolder
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.FileStorages.UserFolder"/>。
        /// </summary>
        public UserFolder()
        {
            //this.SettingsID = SiteSettings.Current.SettingsID;
            this.CreateDate = DateTime.Now;
            this.LastModified = this.CreateDate;
        }

        /// <summary>
        /// 初始化类<see cref="T:Mozlite.FileStorages.UserFolder"/>。
        /// </summary>
        /// <param name="reader">数据库读取器。</param>
        public UserFolder(IDataReader reader)
        {
            this.FolderID = reader.GetInt32("FolderID");
            //this.SettingsID = reader.GetInt32("SettingsID");
            this.UserID = reader.GetInt32("UserID");
            this.ParentID = reader.GetInt32("ParentID");
            this.FolderName = reader.GetString("FolderName");
            this.CreateDate = reader.GetDateTime("CreateDate");
            this.LastModified = reader.GetDateTime("LastModified");
            this.ContentLength = reader.GetInt64("ContentLength");
            this.FileCount = reader.GetInt32("FileCount");
        }

        /// <summary>
        /// 文件夹ID。
        /// </summary>
        public int FolderID { get; set; }

        ///// <summary>
        ///// 网站设置ID。
        ///// </summary>
        //public int SettingsID { get; set; }

        /// <summary>
        /// 用户ID。
        /// </summary>
        public int UserID { get; set; }

        /// <summary>
        /// 父文件夹ID。
        /// </summary>
        public int ParentID { get; set; }

        /// <summary>
        /// 文件夹名称。
        /// </summary>
        public string FolderName { get; set; }

        /// <summary>
        /// 获取或设置文件夹创建日期。
        /// </summary>
        public DateTime CreateDate { get; set; }

        /// <summary>
        /// 获取或设置最后修改日期。
        /// </summary>
        public DateTime LastModified { get; set; }

        /// <summary>
        /// 文件大小。
        /// </summary>
        public long ContentLength { get; set; }

        /// <summary>
        /// 文件数量。
        /// </summary>
        public int FileCount { get; set; }

        private UserFolder parent;
        /// <summary>
        /// 获取父文件夹实例对象。
        /// </summary>
        public UserFolder Parent
        {
            get
            {
                if (ParentID != 0 && parent == null)
                    Storages.GetFolders().TryGetValue(ParentID, out parent);
                return parent;
            }
        }

        private List<UserFolder> children;
        /// <summary>
        /// 获取子文件夹。
        /// </summary>
        public List<UserFolder> Children
        {
            get
            {
                if (children == null)
                {
                    children = new List<UserFolder>();
                    var folders = from folder in Storages.GetFolders().Values
                                  where folder.ParentID == this.FolderID
                                  select folder;
                    if (folders != null)
                    {
                        children = folders.ToList<UserFolder>();
                    }
                }
                return children;
            }
        }

        private List<UserFile> files;
        /// <summary>
        /// 获取当前文件夹的文件列表。
        /// </summary>
        public List<UserFile> Files
        {
            get
            {
                if (files == null && !Storages.GetFiles().TryGetValue(this.FolderID, out files))
                    files = new List<UserFile>();
                return files;
            }
        }

        /// <summary>
        /// 更新子目录和文件。
        /// </summary>
        public void Refresh() {
            this.children = null;
            this.files = null;
        }

        /// <summary>
        /// 判断是否为空。
        /// </summary>
        public bool IsEmpty {
            get {
                return (Children == null || Children.Count == 0) && (Files == null || Files.Count == 0);
            }
        }
    }
}